<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="C++,数据结构,">










<meta name="description" content="数据结构课设报告1． 设计方案">
<meta name="keywords" content="C++,数据结构">
<meta property="og:type" content="article">
<meta property="og:title" content="实现数据结构dos系统">
<meta property="og:url" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/index.html">
<meta property="og:site_name" content="Yu&#39;s Home">
<meta property="og:description" content="数据结构课设报告1． 设计方案">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.jpg">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528951391042.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528952033074.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528952207864.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528952855802.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528953148654.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528953287141.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528953337914.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528953521869.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/1528899426220.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/%E7%BB%98%E5%9B%BE1.jpg">
<meta property="og:updated_time" content="2023-02-23T12:31:14.863Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="实现数据结构dos系统">
<meta name="twitter:description" content="数据结构课设报告1． 设计方案">
<meta name="twitter:image" content="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '9PFXHHRD2G',
      apiKey: '45293228d8a5cecc1c0826fb3d34a130',
      indexName: 'blog',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"没有找到任何结果: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/">





  <title>实现数据结构dos系统 | Yu's Home</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1746d8de4fb78edd21f9004d348fa6be";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Yu's Home</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">管它真理无穷</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于Yu
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Yu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/xiaomai.png">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Yu's Home">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">实现数据结构dos系统</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-14T13:31:01+08:00">
                2018-06-14
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
              <i class="fa fa-comment-o"></i>
              </span>
              <a href="/2018/06/14/实现数据结构dos系统/#comments" itemprop="discussionUrl">
              <span class="post-comments-count valine-comment-count" data-xid="/2018/06/14/实现数据结构dos系统/" itemprop="commentCount"></span>
              </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  5,478
                </span>
              

              

              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="数据结构课设报告"><a href="#数据结构课设报告" class="headerlink" title="数据结构课设报告"></a>数据结构课设报告</h1><h2 id="1．-设计方案"><a href="#1．-设计方案" class="headerlink" title="1． 设计方案"></a>1． 设计方案</h2><p><img src="/2018/06/14/实现数据结构dos系统/%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.jpg" alt="算法与数据结构"></p>
<a id="more"></a> 

<h3 id="1-单链表的基本操作及应用"><a href="#1-单链表的基本操作及应用" class="headerlink" title="1.单链表的基本操作及应用"></a>1.单链表的基本操作及应用</h3><table>
<thead>
<tr>
<th align="center">序号</th>
<th align="center">名称</th>
<th align="center">函数名</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">创建链表</td>
<td align="center">LinkList::LinkList()</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">插入结点</td>
<td align="center">Create_List(int i)</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">删除结点</td>
<td align="center">Delete_List_Head();  Delete_List_End();   Delet_List_Point(int i, int &amp;e);   DeleteAll();</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">查找结点</td>
<td align="center">Find(int i)</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">显示链表</td>
<td align="center">print()</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">通讯录（查找联系人）</td>
<td align="center">Address_books::ElemType Find(string info)</td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">通讯录（删除联系人）</td>
<td align="center">Address_books::Delete()</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">通讯录（修改联系人）</td>
<td align="center">Address_books::Change_contact()</td>
</tr>
</tbody></table>
<h3 id="2-栈的基本操作及应用"><a href="#2-栈的基本操作及应用" class="headerlink" title="2.栈的基本操作及应用"></a>2.栈的基本操作及应用</h3><table>
<thead>
<tr>
<th align="center">序号</th>
<th align="center">名称</th>
<th align="center">函数名</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">初始化栈</td>
<td align="center">InitStack(Stack &amp;s)</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">进栈</td>
<td align="center">Push(Stack &amp;s, int e, int i)</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">出栈</td>
<td align="center">Pop(Stack &amp;s, int &amp;e, int i)</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">取栈顶元素</td>
<td align="center">GetTop(Stack s, int &amp;e)</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">表达式求值（最终运算操作）</td>
<td align="center">EvalueateExpression(const char *expression)</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">表达式求值（对操作数进行四则运算）</td>
<td align="center">operate(int a, char theta, int b)</td>
</tr>
</tbody></table>
<h3 id="3-数组的基本操作及应用"><a href="#3-数组的基本操作及应用" class="headerlink" title="3.数组的基本操作及应用"></a>3.数组的基本操作及应用</h3><table>
<thead>
<tr>
<th align="center">序号</th>
<th align="center">名称</th>
<th align="center">函数名</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">以三元组方式创建矩阵</td>
<td align="center">CreateSMtrix(TSMatrix &amp; m)</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">显示三元组</td>
<td align="center">PrintSMtrix(TSMatrix m)</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">矩阵乘法</td>
<td align="center">MultSMatrix(TSMatrix A, TSMatrix B, TSMatrix &amp; C)</td>
</tr>
</tbody></table>
<h3 id="4-二叉树的基本操作"><a href="#4-二叉树的基本操作" class="headerlink" title="4.二叉树的基本操作"></a>4.二叉树的基本操作</h3><table>
<thead>
<tr>
<th align="center">序号</th>
<th align="center">名称</th>
<th align="center">函数名</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">创建二叉树</td>
<td align="center">CreateBiTree(BiTree &amp;T)</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">先序遍历二叉树</td>
<td align="center">PreOrderTraverse(BiTree T, Status(*Visit)(TElemType e))</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">中序遍历二叉树</td>
<td align="center">InOrderTraverse(BiTree T, Status(*Visit)(TElemType e))</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">后序遍历二叉树</td>
<td align="center">PostOrderTraverse(BiTree T, Status(*Visit)(TElemType e))</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">计算叶子结点数</td>
<td align="center">BiTree_leaf_nodes(BiTree T)</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">树的深度</td>
<td align="center">BiTree_depth(BiTree T)</td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">结点的双亲</td>
<td align="center">BiTree_parents(BiTree T, char e)</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">结点的兄弟</td>
<td align="center">BiTree_lbrothers(BiTree T, char e)；BiTree_rbrothers(BiTree T, char e)</td>
</tr>
</tbody></table>
<h3 id="5-图的基本操作"><a href="#5-图的基本操作" class="headerlink" title="5.图的基本操作"></a>5.图的基本操作</h3><table>
<thead>
<tr>
<th align="center">序号</th>
<th align="center">名称</th>
<th align="center">函数名</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">有向图的创建</td>
<td align="center">CreateDG(ALGraph &amp;G)</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">无向图的创建</td>
<td align="center">CreateUDG(ALGraph &amp;G)</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">有向网的创建</td>
<td align="center">CreateDN(ALGraph &amp;G)</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">无向网的创建</td>
<td align="center">CreateUDN(ALGraph &amp;G)</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">打印邻接表</td>
<td align="center">Print_vertices(ALGraph &amp;G)</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">深度搜索</td>
<td align="center">DFS(ALGraph G, int v)</td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">广度搜索</td>
<td align="center">BFSTraverse(ALGraph G)</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">拓扑排序</td>
<td align="center">Topological_sort(ALGraph G)</td>
</tr>
</tbody></table>
<h2 id="2．-实现过程"><a href="#2．-实现过程" class="headerlink" title="2． 实现过程"></a>2． 实现过程</h2><h4 id="1-单链表的应用（通讯录）"><a href="#1-单链表的应用（通讯录）" class="headerlink" title="1.单链表的应用（通讯录）"></a>1.单链表的应用（通讯录）</h4><p>由于通讯录的实现包含了单链表的基本操作，这里只讨论通讯录的实现</p>
<p>①定义一个通讯录类Address_books</p>
<p>在类中定义结构体Book包括：姓名name，定义一个string类型来存储好友姓名。</p>
<p>​              电话号tel，定义一个string类型来存储好友电话号。</p>
<p> 在类中定义结构体Node，在Node结构体中定义一个Book型变量，定义一个Node型指针指向该结构体。</p>
<p>并在类中添加Node*类型的head成员变量</p>
<p>②在构造函数中进行初始化 Address_books()</p>
<p>Address_books()函数中，即初始化head头结点</p>
<p>③定义一个插入结点（联系人）的函数Create_contact()</p>
<p>Create_contact()函数中，当位置参数传入函数体中，首先判断传进的位置参数是否合理，判断合理之后，去要找该位置，断开链子，将结点插入其中。</p>
<p>④定义一个删除结点（联系人）的函数Delete()</p>
<p>Delete()函数中，与插入函数类似，首先判断传入的位置参数是否合理，找到该结点，删除结点之后，将前一个结点的指针域指向下一个结点的空间。</p>
<p>⑤定义一个查找结点（联系人）的函数Find()</p>
<p>Find()函数中，首先输入好友的姓名，判断字符串是否相等找到联系人，并输出该联系人的相关信息。</p>
<p>⑥显示函数print()</p>
<p>print()函数需要传入一个指向链表的头指针，判断头指针不为空时，输出链表里所有的结点元素（联系人信息）。</p>
<p>⑦显示链表菜单函数Show_Books()</p>
<p>Show_Books()函数的菜单项是通过switch()语句实现选择功能。</p>
<h4 id="2-栈的基本操作及应用-1"><a href="#2-栈的基本操作及应用-1" class="headerlink" title="2.栈的基本操作及应用"></a>2.栈的基本操作及应用</h4><p>①定义一个栈的结构体</p>
<p>结构体中包括：栈底指针<em>base，栈顶指针</em>top。</p>
<p>②定义一个进栈函数</p>
<p>​    用Push()函数，将元素压入栈低，S.top++；，栈顶指针加一。</p>
<p>③定义一个出栈函数</p>
<p>​    将后进入的元素弹出，e = *(S.top-1); S.top–;栈顶指针减一。</p>
<p>④定义一个取栈顶元素</p>
<p>保存栈顶元素，并把值带回主函数，top指针不变。</p>
<p>⑤表达式求值的应用</p>
<p>定义一个操作符栈，一个操作数栈，建议优先级表，对输入的表达式进行条件入栈出栈操作，实现表达式求值的功能</p>
<h4 id="3-数组的基本操作及应用-1"><a href="#3-数组的基本操作及应用-1" class="headerlink" title="3.数组的基本操作及应用"></a>3.数组的基本操作及应用</h4><p>①  创建三元组</p>
<p>要求用户输入稀疏矩阵的行数列数，非零元个数，然后进行判断接下来的输入是否合理和输入完成，把输入的值依次加入Triple data[MAXSIZE]中</p>
<p>②  显示三元组</p>
<p>循环打印三元组的非零元行、列下标及其数据。</p>
<p>③  矩阵乘法（应用）</p>
<p>为了简化矩阵乘法运算，首先找到矩阵中的非零元，存储于三元组数组data[MAXRL+1]中，然后再将数组中对应行、列的非零元相乘、再相加，将和及对应的行、列下标存储于三元组W中，通过打印三元组函数PrintSMtrix(TSMatrix m)，将矩阵相乘结果输出。得到非零元所在的行、列下标及其数据。</p>
<h4 id="4-二叉树的基本操作-1"><a href="#4-二叉树的基本操作-1" class="headerlink" title="4.二叉树的基本操作"></a>4.二叉树的基本操作</h4><p>①  创建二叉树</p>
<p>构造一个链式存储结构体，其中包括数据域、左、右孩子指针。设计一个以先序方式创建二叉树，通过递归调用CreateBiTree()，T-&gt;data=ch;生成根结点CreateBiTree(T-&gt;lchild);构造左子树CreateBiTree(T-&gt;rchild); 构造右子树，完成构造二叉树。</p>
<p>②  遍历二叉树（先序、中序、后序）</p>
<p>先序遍历二叉树PreOrderTraverse()，通过设计Visit()显示函数，递归InOrderTraverse(T-&gt;lchild,Visit)遍历左子树，递归InOrderTraverse(T-&gt;rchild,Visit)遍历右子树。访问顺序：根、左、右。</p>
<p>中序遍历二叉树InOrderTraverse()，递归PostOrderTraverse(T-&gt;lchild,Visit）遍历左子树，递归PostOrderTraverse(T-&gt;rchild,Visit）遍历右子树。访问顺序：左、根、右。</p>
<p>后序遍历二叉树PostOrderTraverse ()，递归PostOrderTraverse (T-&gt;lchild,Visit）遍历左子树，递归PostOrderTraverse (T-&gt;rchild,Visit）遍历右子树。访问顺序：左、右、根。</p>
<p>③  计算叶子结点个数及树的深度</p>
<p>叶子节点个数：递归调用BiTree_leaf_nodes()，当结点的左右孩子都无，</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> BiTree_leaf_nodes(T-&gt;lchild) + BiTree_leaf_nodes(T-&gt;rchild) + <span class="number">1</span>;</span><br></pre></td></tr></table></figure>

<p>否则</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> BiTree_leaf_nodes(T-&gt;lchild) + BiTree_leaf_nodes(T-&gt;rchild);</span><br></pre></td></tr></table></figure>

<p>树的深度：递归调用Depth()，输出左子树、右子树中最大值。</p>
<p>④  查找指定结点的双亲</p>
<p>非递归BiTree_parents()函数，遍历根结点的左右子树，获取所有结点的情况，在最后进行打印</p>
<p>⑤  查找指定结点的兄弟</p>
<p>递归调用找左兄弟BiTree_lbrothers()函数，遍历结点的左右子树，看双亲，输出结果。</p>
<p>递归调用找右兄弟BiTree_rbrothers()函数，遍历结点的左右子树，看双亲，输出结果。</p>
<h4 id="5-图的基本操作-1"><a href="#5-图的基本操作-1" class="headerlink" title="5.图的基本操作"></a>5.图的基本操作</h4><p>①创建无向图</p>
<p>先构造邻接矩阵结构体AdjList vertices; 函数CreateDG(ALGraph &amp; G)要求先输入顶点数与边数，用于输入判断。然后再输入所有顶点信息和边的信息存入邻接表vertices中，输入完成后自动提示，并打印对应的邻接表</p>
<p>②创建有向图</p>
<p>③创建无向网</p>
<p>网相对于图多了一个权重，表结点结构体中的info用于存储权值，在创建网的函数中输入权值的信息。并在创建完成后打印邻接表</p>
<p>④创建有向网</p>
<p>⑤DFS和BFS遍历</p>
<p>深度优先遍历图：构造访问标记数组visited[]，递归调用DFS()，直至图中所有结点被访问来实现深度遍历图。</p>
<p>广度优先遍历图：类似于树的层序遍历，同时也需要构造访问数组visited[]，个人是调用STL中的队列queue来实现。</p>
<p>⑥拓扑排序   </p>
<p>Topological_sort(ALGraph G)；在头结点的结构体中加了一个int型的conut成员用于存储结点的入度，入度的获取在创建图(网)的函数中实现。先将入度为0的结点入栈，然后实现拓扑排序的算法原理，计算出最后的顶点数，若小于当前图的顶点数，则说明有回路，打印出结果，若无回路，提示并打印拓扑有序序列</p>
<h2 id="3．-实现代码"><a href="#3．-实现代码" class="headerlink" title="3． 实现代码"></a>3． 实现代码</h2><h4 id="通讯录的数据结构及部分实现函数"><a href="#通讯录的数据结构及部分实现函数" class="headerlink" title="通讯录的数据结构及部分实现函数"></a>通讯录的数据结构及部分实现函数</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Book</span> &#123;</span></span><br><span class="line">		<span class="built_in">string</span> name;</span><br><span class="line">		<span class="built_in">string</span> tel;</span><br><span class="line">	&#125;;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Node</span> &#123;</span></span><br><span class="line">		Book data;</span><br><span class="line">		Node * next;</span><br><span class="line">	&#125;;</span><br><span class="line">	Node *head;</span><br><span class="line">	Node *_temp;	<span class="comment">//用作临时结点指针方便函数调用</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> Address_books::Change_contact()<span class="comment">//修改联系人信息</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">string</span> info;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"输入你要修改的联系人的相关信息(姓名或联系方式):"</span>;</span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; info;</span><br><span class="line">	<span class="keyword">if</span> (!Find(info))</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"输入该联系人新的姓名:"</span>;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; info;</span><br><span class="line">		<span class="keyword">this</span>-&gt;_temp-&gt;next-&gt;data.name = info;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"输入该联系人新的电话号码:"</span>;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; info;</span><br><span class="line">		<span class="keyword">this</span>-&gt;_temp-&gt;next-&gt;data.tel = info;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span> &lt;&lt; <span class="string">"联系人信息更新成功!"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> Address_books::Delete()<span class="comment">//删除联系人</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">string</span> info;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"输入你要删除的联系人的相关信息(姓名或联系方式):"</span>;</span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; info;</span><br><span class="line">	<span class="keyword">if</span> (!Find(info))</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"确定删除联系人 "</span>&lt;&lt; _temp-&gt;next-&gt;data.name &lt;&lt;<span class="string">" 的信息吗?	(y or n)?:"</span>;</span><br><span class="line">		<span class="keyword">char</span> c;</span><br><span class="line">		<span class="built_in">cin</span> &gt;&gt; c;</span><br><span class="line">		<span class="keyword">if</span> (c == <span class="string">'y'</span> || c == <span class="string">'Y'</span>) &#123;</span><br><span class="line">			Node *q = _temp-&gt;next;</span><br><span class="line">			<span class="keyword">this</span>-&gt;_temp-&gt;next = _temp-&gt;next-&gt;next;</span><br><span class="line">			<span class="keyword">delete</span> q;</span><br><span class="line">			q = <span class="literal">NULL</span>;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span> &lt;&lt; <span class="string">"联系人信息删除成功!"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			<span class="keyword">return</span>;	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> Address_books::Find_contact()<span class="comment">//查找联系人</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">string</span> info;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"输入你要查找的联系人的相关信息(姓名或联系方式):"</span>;</span><br><span class="line">	<span class="built_in">cin</span> &gt;&gt; info;</span><br><span class="line">	Find(info);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ElemType Address_books::Find(<span class="built_in">string</span> info)</span><br><span class="line">&#123;</span><br><span class="line">	Node *temp = head-&gt;next;</span><br><span class="line">	<span class="keyword">this</span>-&gt;_temp = head;	<span class="comment">//查找第一个结点时没有进入while循环_temp为空的情况</span></span><br><span class="line">	<span class="keyword">if</span> (head-&gt;next == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"空电话本,没有联系人!"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">return</span> FALSE;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">while</span> (temp != <span class="literal">NULL</span> &amp;&amp; temp-&gt;data.name != info &amp;&amp; temp-&gt;data.tel!=info)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">this</span>-&gt;_temp = temp;	<span class="comment">//_temp指向信息结点的前一个,方便删除</span></span><br><span class="line">		temp = temp-&gt;next;	</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (!temp)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"查找失败，相关联系人不存在！"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">return</span> FALSE;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"查找成功，相关联系人信息如下: "</span> &lt;&lt; <span class="built_in">endl</span></span><br><span class="line">		&lt;&lt; <span class="string">"姓名:"</span> &lt;&lt; temp-&gt;data.name &lt;&lt; <span class="built_in">endl</span> &lt;&lt; <span class="string">"电话号码:"</span> &lt;&lt; temp-&gt;data.tel &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">return</span> TRUE;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="表达式求值的数据结构及部分函数"><a href="#表达式求值的数据结构及部分函数" class="headerlink" title="表达式求值的数据结构及部分函数"></a>表达式求值的数据结构及部分函数</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义操作数栈</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">OPERANDSTACK</span> &#123;</span></span><br><span class="line">	<span class="keyword">int</span> *base;</span><br><span class="line">	<span class="keyword">int</span> *top;</span><br><span class="line">	<span class="keyword">int</span> stacksize;</span><br><span class="line">&#125;OPERANDSTACK;</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义操作符栈</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">OPERATORSTACK</span> &#123;</span></span><br><span class="line">	<span class="keyword">char</span> *base;</span><br><span class="line">	<span class="keyword">char</span> *top;</span><br><span class="line">	<span class="keyword">int</span> stacksize;</span><br><span class="line">&#125;OPERATORSTACK;</span><br><span class="line"></span><br><span class="line"><span class="comment">//判断c是否是一个运算操作符  </span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isOperator</span><span class="params">(<span class="keyword">char</span> c)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">switch</span> (c) &#123;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'+'</span>:</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'-'</span>:</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'*'</span>:</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'/'</span>:</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'('</span>:</span><br><span class="line">	<span class="keyword">case</span> <span class="string">')'</span>:</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'#'</span>:	<span class="comment">//将'#'作为运算符栈opter的栈底元素，判断表达式是否求值完毕</span></span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">default</span>:</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//如果以*c开始的操作数后仍是数字就加上前面的数字进行合并</span></span><br><span class="line"><span class="function"><span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">getOpnd</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *c, <span class="keyword">int</span> *op)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//获得以*c开始的操作数，返回后c为操作符  </span></span><br><span class="line">	<span class="keyword">int</span> sum = <span class="number">0</span>, temp;</span><br><span class="line">	<span class="keyword">while</span> (!isOperator(*c))</span><br><span class="line">	&#123;</span><br><span class="line">		temp = *c - <span class="string">'0'</span>;</span><br><span class="line">		sum = sum * <span class="number">10</span> + temp;</span><br><span class="line">		c++;</span><br><span class="line">	&#125;</span><br><span class="line">	*op = sum;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> c;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">char</span> op[<span class="number">7</span>][<span class="number">7</span>] =</span><br><span class="line">&#123;</span><br><span class="line">&#123; <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span> &#125;,</span><br><span class="line">&#123; <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span> &#125;,</span><br><span class="line">&#123; <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span> &#125;,</span><br><span class="line">&#123; <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span> &#125;,</span><br><span class="line">&#123; <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'='</span>, <span class="string">' '</span> &#125;,</span><br><span class="line">&#123; <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span>, <span class="string">' '</span>, <span class="string">'&gt;'</span>, <span class="string">'&gt;'</span> &#125;,</span><br><span class="line">&#123; <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">'&lt;'</span>, <span class="string">' '</span>, <span class="string">'='</span> &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//优先级的处理</span></span><br><span class="line"><span class="function"><span class="keyword">char</span> <span class="title">precede</span><span class="params">(<span class="keyword">char</span> op1, <span class="keyword">char</span> op2)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> i, j; <span class="comment">//用来记录优先级坐标点位置</span></span><br><span class="line">	<span class="keyword">switch</span> (op1) &#123;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'+'</span>: i = <span class="number">0</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'-'</span>: i = <span class="number">1</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'*'</span>: i = <span class="number">2</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'/'</span>: i = <span class="number">3</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'('</span>: i = <span class="number">4</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">')'</span>: i = <span class="number">5</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'#'</span>: i = <span class="number">6</span>; <span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">switch</span> (op2) &#123;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'+'</span>: j = <span class="number">0</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'-'</span>: j = <span class="number">1</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'*'</span>: j = <span class="number">2</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'/'</span>: j = <span class="number">3</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'('</span>: j = <span class="number">4</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">')'</span>: j = <span class="number">5</span>; <span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="string">'#'</span>: j = <span class="number">6</span>; <span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> op[i][j];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="稀疏矩阵的数据结构及乘法实现"><a href="#稀疏矩阵的数据结构及乘法实现" class="headerlink" title="稀疏矩阵的数据结构及乘法实现"></a>稀疏矩阵的数据结构及乘法实现</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*---稀疏矩阵的三元组顺序表存储表示---*/</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Triple</span>&#123;</span></span><br><span class="line">	<span class="keyword">int</span> i, j;		<span class="comment">//非零元的行下标和列下标</span></span><br><span class="line">	ElemType e;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">TSMatrix</span> &#123;</span></span><br><span class="line">	Triple data[MAXSIZE];		<span class="comment">//非零元的三元组表</span></span><br><span class="line">	<span class="keyword">int</span> row, col, total;		<span class="comment">//矩阵的行数、列数、非零元个数</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">Status <span class="title">MultSMatrix</span><span class="params">(TSMatrix A, TSMatrix B, TSMatrix &amp; C)</span></span></span><br><span class="line"><span class="function"></span>&#123;<span class="comment">// 将稀疏矩阵A和B进行乘法运算后赋给矩阵C</span></span><br><span class="line"><span class="comment">// 原理:用A的一行的元素与B的每一列的元素对应相乘再相加求解</span></span><br><span class="line"><span class="comment">// 时间复杂度:O(A.row*B.col+A.total/A.col*B.total)</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (A.col != B.row)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"A的列标不等于B的行标!计算失败"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">return</span> ERROR;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	C.row = A.row;</span><br><span class="line">	C.col = B.col;</span><br><span class="line">	C.total = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*memset()用于逐字节的把整个数组设置为一个指定的值*/</span></span><br><span class="line">	<span class="keyword">int</span>* Anum = <span class="keyword">new</span> <span class="keyword">int</span>[A.row + <span class="number">1</span>];</span><br><span class="line">	<span class="built_in">memset</span>(Anum, <span class="number">0</span>, (A.row + <span class="number">1</span>) * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span><br><span class="line">	<span class="keyword">int</span>* Arpos = <span class="keyword">new</span> <span class="keyword">int</span>[A.row + <span class="number">1</span>];</span><br><span class="line">	<span class="built_in">memset</span>(Arpos, <span class="number">0</span>, (A.row + <span class="number">1</span>) * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span>* Bnum = <span class="keyword">new</span> <span class="keyword">int</span>[B.row + <span class="number">1</span>];</span><br><span class="line">	<span class="built_in">memset</span>(Bnum, <span class="number">0</span>, (B.row + <span class="number">1</span>) * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span><br><span class="line">	<span class="keyword">int</span>* Brpos = <span class="keyword">new</span> <span class="keyword">int</span>[B.row + <span class="number">1</span>];</span><br><span class="line">	<span class="built_in">memset</span>(Brpos, <span class="number">0</span>, (B.row + <span class="number">1</span>) * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> i, arow, p, p_up, brow, q, q_up, ccol;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; A.total; i++)</span><br><span class="line">		Anum[A.data[i].i]++;</span><br><span class="line">	<span class="comment">//计算A中每行的非零元在data中的起始下标</span></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= A.row; i++)</span><br><span class="line">		Arpos[i] = Arpos[i - <span class="number">1</span>] + Anum[i - <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; B.total; i++)</span><br><span class="line">		Bnum[B.data[i].i]++;</span><br><span class="line">	<span class="comment">//计算B中每行的非零元在data中的起始下标</span></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= B.row; i++)</span><br><span class="line">		Brpos[i] = Brpos[i - <span class="number">1</span>] + Bnum[i - <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span>* mult = <span class="keyword">new</span> <span class="keyword">int</span>[B.col + <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (arow = <span class="number">0</span>; arow &lt;= A.row; arow++)</span><br><span class="line">	&#123;</span><br><span class="line"></span><br><span class="line">		p_up = arow &lt; A.row ? Arpos[arow + <span class="number">1</span>] : A.total;	<span class="comment">//条件表达式，若arow&lt;A.row,执行:前一条语句，否则后一条</span></span><br><span class="line">		<span class="keyword">for</span> (p = Arpos[arow]; p &lt; p_up; p++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">memset</span>(mult, <span class="number">0</span>, (B.col + <span class="number">1</span>) * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span><br><span class="line"></span><br><span class="line">			brow = A.data[p].j;</span><br><span class="line">			q_up = brow &lt; B.row ? Brpos[brow + <span class="number">1</span>] : B.total;</span><br><span class="line">			<span class="keyword">for</span> (q = Brpos[brow]; q &lt; q_up; q++)</span><br><span class="line">			&#123;</span><br><span class="line">				mult[B.data[q].j] += A.data[p].e * B.data[q].e;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">for</span> (ccol = <span class="number">1</span>; ccol &lt;= B.col; ccol++)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">if</span> (<span class="built_in">fabs</span>(mult[ccol])&gt;<span class="number">1e-7</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					C.data[C.total].i = arow;</span><br><span class="line">					C.data[C.total].j = ccol;</span><br><span class="line">					C.data[C.total].e = mult[ccol];</span><br><span class="line"></span><br><span class="line">					C.total++;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">delete</span>[] Anum;</span><br><span class="line">	<span class="keyword">delete</span>[] Arpos;</span><br><span class="line">	<span class="keyword">delete</span>[] Bnum;</span><br><span class="line">	<span class="keyword">delete</span>[] Brpos;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> OK;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="求二叉树叶子结点及双亲的实现函数"><a href="#求二叉树叶子结点及双亲的实现函数" class="headerlink" title="求二叉树叶子结点及双亲的实现函数"></a>求二叉树叶子结点及双亲的实现函数</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">BiTree_leaf_nodes</span><span class="params">(BiTree T)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (T == <span class="literal">NULL</span>)</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">if</span> (T-&gt;lchild == <span class="number">0</span> &amp;&amp; T-&gt;rchild == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> BiTree_leaf_nodes(T-&gt;lchild) + BiTree_leaf_nodes(T-&gt;rchild) + <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> BiTree_leaf_nodes(T-&gt;lchild) + BiTree_leaf_nodes(T-&gt;rchild);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BiTree_parents</span><span class="params">(BiTree T, <span class="keyword">char</span> e)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> count = <span class="number">0</span>;	<span class="comment">//记录相同值的结点数</span></span><br><span class="line">	<span class="keyword">bool</span> root = <span class="literal">false</span>;	<span class="comment">//记录结点是根节点的情况</span></span><br><span class="line">	<span class="built_in">vector</span>&lt;<span class="keyword">char</span>&gt; v1;	<span class="comment">//存储具有双亲结点的双亲结点值</span></span><br><span class="line">	<span class="built_in">stack</span>&lt;BiTree&gt; S;	<span class="comment">//非递归先序遍历</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (T == <span class="literal">NULL</span>)</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">if</span> (T-&gt;data == e) &#123;</span><br><span class="line">		count++;</span><br><span class="line">		root = <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">while</span> (!S.empty() || T)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">while</span> (T)</span><br><span class="line">		&#123;</span><br><span class="line">			S.push(T);</span><br><span class="line">			<span class="keyword">if</span> ((T-&gt;lchild &amp;&amp; T-&gt;lchild-&gt;data == e) || (T-&gt;rchild &amp;&amp; T-&gt;rchild-&gt;data == e)) &#123;</span><br><span class="line">				count++;</span><br><span class="line">				v1.push_back(T-&gt;data);</span><br><span class="line">			&#125;	</span><br><span class="line">			T = T-&gt;lchild;</span><br><span class="line">		&#125;</span><br><span class="line">		T = S.top(); S.pop();</span><br><span class="line">		T = T-&gt;rchild;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"该值对应的结点共有 "</span> &lt;&lt; count &lt;&lt; <span class="string">" 个,对应的双亲为:"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">int</span> n = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">if</span> (root) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"第"</span> &lt;&lt; ++n &lt;&lt;<span class="string">"个结点为根结点,无双亲结点!"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> i : v1) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"第"</span> &lt;&lt; ++n &lt;&lt; <span class="string">"个结点的双亲结点:"</span> &lt;&lt; i &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="图的拓扑排序实现"><a href="#图的拓扑排序实现" class="headerlink" title="图的拓扑排序实现"></a>图的拓扑排序实现</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Topological_sort</span><span class="params">(ALGraph G)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">	<span class="keyword">int</span> i, k;</span><br><span class="line">	<span class="comment">//入度为0入栈</span></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; G.vexnum; ++i)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (!G.vertices[i].count)<span class="comment">//把入度为零的点先放入栈中</span></span><br><span class="line">			s.push(i);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">	ArcNode *p = <span class="keyword">new</span> ArcNode;</span><br><span class="line">	<span class="keyword">while</span> (!s.empty())</span><br><span class="line">	&#123;</span><br><span class="line">		i = s.top();</span><br><span class="line">		s.pop();</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; G.vertices[i].data &lt;&lt; <span class="string">"-"</span>;</span><br><span class="line">		count++;</span><br><span class="line">		<span class="keyword">for</span> (p = G.vertices[i].firstarc; p ; p = p-&gt;nextarc)</span><br><span class="line">		&#123;</span><br><span class="line">			k = p-&gt;adjvex;</span><br><span class="line">			<span class="keyword">if</span> (!(--G.vertices[k].count))</span><br><span class="line">				s.push(k);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (count &lt; G.vexnum)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"该图(网)有回路！"</span>;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"该图(网)无回路"</span>;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4．-测试"><a href="#4．-测试" class="headerlink" title="4． 测试"></a>4． 测试</h2><h3 id="链表"><a href="#链表" class="headerlink" title="链表"></a>链表</h3><p><img src="/2018/06/14/实现数据结构dos系统/1528951391042.png" alt="1528951391042"></p>
<p><img src="/2018/06/14/实现数据结构dos系统/1528952033074.png" alt="1528952033074"></p>
<h3 id="栈的操作"><a href="#栈的操作" class="headerlink" title="栈的操作"></a>栈的操作</h3><p><img src="/2018/06/14/实现数据结构dos系统/1528952207864.png" alt="1528952207864"></p>
<p><img src="/2018/06/14/实现数据结构dos系统/1528952855802.png" alt="1528952855802"></p>
<h3 id="稀疏矩阵乘法"><a href="#稀疏矩阵乘法" class="headerlink" title="稀疏矩阵乘法"></a>稀疏矩阵乘法</h3><p><img src="/2018/06/14/实现数据结构dos系统/1528953148654.png" alt="1528953148654"></p>
<h3 id="二叉树"><a href="#二叉树" class="headerlink" title="二叉树"></a>二叉树</h3><p><img src="/2018/06/14/实现数据结构dos系统/1528953287141.png" alt="1528953287141"></p>
<p><img src="/2018/06/14/实现数据结构dos系统/1528953337914.png" alt="1528953337914"></p>
<h3 id="图的操作"><a href="#图的操作" class="headerlink" title="图的操作"></a>图的操作</h3><p><img src="/2018/06/14/实现数据结构dos系统/1528953521869.png" alt="1528953521869"></p>
<h2 id="5．-结论"><a href="#5．-结论" class="headerlink" title="5． 结论"></a>5． 结论</h2><p>这个系统人性化方面和界面做得还算不错，但还是有不足的地方，例如在每个函数中得到exit字符的话会退出当前函数以免每次输入错误无法返回上一级；console的清屏效果也没有实现，因为调用system(‘cls’)来实现的话不会保存之前的记录，我想实现linux中的clear会把屏幕下拉的效果，但由于时间关系这些点都没有实现。对于这次课设的设计框架方面也有不足，没有用到面向对象的多态性；虽说技术无优劣，面向过程在这次设计中的应用设计非常简单方便，但实现面向对象编程才是C++的难点，因为其标准之多，对培养语言思维有很大的帮助，在今后还要多多编写有关项目。</p>
<h2 id="6．-难点与收获"><a href="#6．-难点与收获" class="headerlink" title="6． 难点与收获"></a>6． 难点与收获</h2><h3 id="•-难点"><a href="#•-难点" class="headerlink" title="•   难点"></a>•   难点</h3><p><strong>1.</strong> 在我完成的三个应用中，个人认为表达式求解的难度较其余两个(通讯录和稀疏矩阵乘法)更大，要考虑到1数值运算，2运算符优先级，3进栈出栈的条件，4特殊情况(连续两个字符都是操作数要进行合并)，对于这个应用来说还有改进的地方：输入字符中带有空格的处理；输入错误的提示信息等等</p>
<p><strong>2.</strong> 第一个应用(通讯录的实现)，用链表来实现通讯录的原理很简单，即把链表的data结构再用一个数据结构(包含要输入的信息)来表示。因为考虑到代码的精简性，实现过程中唯一的一个问题就是函数的相互调用。比如说当我要删除联系人或修改联系人的时候都会用到查找这个函数，但是接下来如何实现删除和修改，就需要获取指向这个联系人的指针，所以我在类中定义了一个私有成员Node *_temp;    用于指向查找到的前一个结点(因为对于删除操作，要获取前一个结点的指针)。这个简易通讯录还有改进的地方：利用正则表达式实现”电话输入格式判断”，”联想搜索”；多链表进行分组管理等等</p>
<p><strong>3.</strong> 在二叉树的设计中，递归的灵活运用可以算是一个难点，个人总结出了一些自己的想法：</p>
<p>把原有的问题分解为一个新问题，而新问题又要用原有问题的解决方案 </p>
<p>在实现的过程中，它采用了分治法的思想：即将整体分割成部分，并总是从最小的部分（基本部分）开始入手（输出），其背后的原理在于 当整体递归到部分时，会保留整体的信息，部分满足条件输出的结果会被回溯给整体使用，从而使得整体输出结果。</p>
<p>例如在求树的深度时：我们都知道将左子树与右子树的深度进行比较，深度大的子树深度+1即为整颗二叉树的深度，从最简单的二叉树来看也满足这个条件，而在求左右子树的深度时也是一样的方法，所以我们可以利用递归的方式，一条语句即可实现树的深度求解</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> (BiTree_depth(T-&gt;lchild) &gt; BiTree_depth(T-&gt;rchild)) ? (BiTree_depth(T-&gt;lchild) + <span class="number">1</span>) : (BiTree_depth(T-&gt;rchild) + <span class="number">1</span>);</span><br></pre></td></tr></table></figure>

<p>其他的 遍历，求叶子结点，双亲，兄弟都是用到了同样的思想。</p>
<p>这里我要着重说一下双亲(兄弟)的求解，递归固然方便，但也削弱了对流程的可控性。在双亲的求解中可能会出现以下两种情况：1.根节点，无双亲；2.有多个值相同的结点。从人性化方面：我会在最后输出该值对应的结点有几个，然后分别打印出对应结点的情况。如果用递归的话也是可以做到，但无法控制最后一次递归打印信息，可以将获取的信息存到全局静态容器中，再用另一个函数对其进行打印。而如果要在一个函数中实现其功能就必须用非递归算法了，这里我用了栈对其进行循环控制，添加了一个verctor容器存储双亲结点，然后在最后进行打印。实现代码与测试结果如下:</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BiTree_parents</span><span class="params">(BiTree T, <span class="keyword">char</span> e)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> count = <span class="number">0</span>;	<span class="comment">//记录相同值的结点数</span></span><br><span class="line">	<span class="keyword">bool</span> root = <span class="literal">false</span>;	<span class="comment">//记录结点是根节点的情况</span></span><br><span class="line">	<span class="built_in">vector</span>&lt;<span class="keyword">char</span>&gt; v1;	<span class="comment">//存储具有双亲结点的双亲结点值</span></span><br><span class="line">	<span class="built_in">stack</span>&lt;BiTree&gt; S;	<span class="comment">//非递归先序遍历</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (T == <span class="literal">NULL</span>)</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	<span class="keyword">if</span> (T-&gt;data == e) &#123;</span><br><span class="line">		count++;</span><br><span class="line">		root = <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">while</span> (!S.empty() || T)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">while</span> (T)</span><br><span class="line">		&#123;</span><br><span class="line">			S.push(T);</span><br><span class="line">			<span class="keyword">if</span> ((T-&gt;lchild &amp;&amp; T-&gt;lchild-&gt;data == e) || (T-&gt;rchild &amp;&amp; T-&gt;rchild-&gt;data == e)) &#123;</span><br><span class="line">				count++;</span><br><span class="line">				v1.push_back(T-&gt;data);</span><br><span class="line">			&#125;	</span><br><span class="line">			T = T-&gt;lchild;</span><br><span class="line">		&#125;</span><br><span class="line">		T = S.top(); S.pop();</span><br><span class="line">		T = T-&gt;rchild;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">"该值对应的结点共有 "</span> &lt;&lt; count &lt;&lt; <span class="string">" 个,对应的双亲为:"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">int</span> n = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">if</span> (root) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"第"</span> &lt;&lt; ++n &lt;&lt;<span class="string">"个结点为根结点,无双亲结点!"</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> i : v1) &#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">"第"</span> &lt;&lt; ++n &lt;&lt; <span class="string">"个结点的双亲结点:"</span> &lt;&lt; i &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="/2018/06/14/实现数据结构dos系统/1528899426220.png" alt="查找双亲"></p>
<p><strong>4.</strong> 图的邻接表存储表示，涉及到的链表比较多，数据结构间的逻辑也较难理解。我画一个图来表示其间的联系。</p>
<p><img src="/2018/06/14/实现数据结构dos系统/%E7%BB%98%E5%9B%BE1.jpg" alt="绘图1"></p>
<h3 id="•-收获"><a href="#•-收获" class="headerlink" title="•   收获"></a>•   收获</h3><p><strong>1.</strong> 实现了数据结构的dos系统。系统内容包括:带头结点的单链表 2.栈的顺序存储表示 3.稀疏矩阵的三元组顺序表示 4.二叉树的链式结构表示 5.图的邻接表存储表示  及前三项的简单应用</p>
<p><strong>2.</strong> 了解了数据结构的本质:数据的各种逻辑结构和存储结构，以及对数据的各种操作。</p>
<p>熟悉了如何建立一个数据结构和使用它。</p>
<p><strong>3.</strong> 深刻的感受到了数据结构对算法效率的提高</p>
<p><strong>4.</strong> 加强了独自解决问题的能力，培养了程序设计的思路，加深了逻辑推理思维</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>觉得好的话就打赏Ta一瓶冰阔落吧</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Yu 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Yu 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    Yu
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/" title="实现数据结构dos系统">https://yujunjiex.gitee.io/2018/06/14/实现数据结构dos系统/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/C/" rel="tag"># C++</a>
          
            <a href="/tags/数据结构/" rel="tag"># 数据结构</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/05/31/python实现条件编译/" rel="next" title="python实现条件编译">
                <i class="fa fa-chevron-left"></i> python实现条件编译
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/08/16/网页上数学公式显示不正确的解决方案/" rel="prev" title="网页上数学公式显示不正确的解决方案">
                网页上数学公式显示不正确的解决方案 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- Go to www.addthis.com/dashboard to customize your tools -->
<div class="addthis_inline_share_toolbox">
  <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5b03d4a278f58ab9" async="async"></script>
</div>

      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/xiaomai.png" alt="Yu">
            
              <p class="site-author-name" itemprop="name">Yu</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">30</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yujunjiex" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#数据结构课设报告"><span class="nav-number">1.</span> <span class="nav-text">数据结构课设报告</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1．-设计方案"><span class="nav-number">1.1.</span> <span class="nav-text">1． 设计方案</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-单链表的基本操作及应用"><span class="nav-number">1.1.1.</span> <span class="nav-text">1.单链表的基本操作及应用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-栈的基本操作及应用"><span class="nav-number">1.1.2.</span> <span class="nav-text">2.栈的基本操作及应用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-数组的基本操作及应用"><span class="nav-number">1.1.3.</span> <span class="nav-text">3.数组的基本操作及应用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-二叉树的基本操作"><span class="nav-number">1.1.4.</span> <span class="nav-text">4.二叉树的基本操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-图的基本操作"><span class="nav-number">1.1.5.</span> <span class="nav-text">5.图的基本操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2．-实现过程"><span class="nav-number">1.2.</span> <span class="nav-text">2． 实现过程</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-单链表的应用（通讯录）"><span class="nav-number">1.2.0.1.</span> <span class="nav-text">1.单链表的应用（通讯录）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-栈的基本操作及应用-1"><span class="nav-number">1.2.0.2.</span> <span class="nav-text">2.栈的基本操作及应用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-数组的基本操作及应用-1"><span class="nav-number">1.2.0.3.</span> <span class="nav-text">3.数组的基本操作及应用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-二叉树的基本操作-1"><span class="nav-number">1.2.0.4.</span> <span class="nav-text">4.二叉树的基本操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-图的基本操作-1"><span class="nav-number">1.2.0.5.</span> <span class="nav-text">5.图的基本操作</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3．-实现代码"><span class="nav-number">1.3.</span> <span class="nav-text">3． 实现代码</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#通讯录的数据结构及部分实现函数"><span class="nav-number">1.3.0.1.</span> <span class="nav-text">通讯录的数据结构及部分实现函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#表达式求值的数据结构及部分函数"><span class="nav-number">1.3.0.2.</span> <span class="nav-text">表达式求值的数据结构及部分函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#稀疏矩阵的数据结构及乘法实现"><span class="nav-number">1.3.0.3.</span> <span class="nav-text">稀疏矩阵的数据结构及乘法实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#求二叉树叶子结点及双亲的实现函数"><span class="nav-number">1.3.0.4.</span> <span class="nav-text">求二叉树叶子结点及双亲的实现函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#图的拓扑排序实现"><span class="nav-number">1.3.0.5.</span> <span class="nav-text">图的拓扑排序实现</span></a></li></ol></li></ol><li class="nav-item nav-level-2"><a class="nav-link" href="#4．-测试"><span class="nav-number">1.4.</span> <span class="nav-text">4． 测试</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#链表"><span class="nav-number">1.4.1.</span> <span class="nav-text">链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#栈的操作"><span class="nav-number">1.4.2.</span> <span class="nav-text">栈的操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#稀疏矩阵乘法"><span class="nav-number">1.4.3.</span> <span class="nav-text">稀疏矩阵乘法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二叉树"><span class="nav-number">1.4.4.</span> <span class="nav-text">二叉树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图的操作"><span class="nav-number">1.4.5.</span> <span class="nav-text">图的操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5．-结论"><span class="nav-number">1.5.</span> <span class="nav-text">5． 结论</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6．-难点与收获"><span class="nav-number">1.6.</span> <span class="nav-text">6． 难点与收获</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#•-难点"><span class="nav-number">1.6.1.</span> <span class="nav-text">•   难点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#•-收获"><span class="nav-number">1.6.2.</span> <span class="nav-text">•   收获</span></a></li></ol></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heartbeat"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Yu</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">46.7k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a></div>



        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>

<script type="text/javascript" src="/custom/fold_action.js?v="></script>





  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//code.bdstatic.com/npm/leancloud-storage@4.12.0/dist/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'rg0EMRYKYKMhOh1zwqEKS5jz-gzGzoHsz',
        appKey: 'T04zQc0OzQd1uxOqcVomoGqj',
        placeholder: '(= =)~*只有10086嘘寒问暖',
        avatar:'mm',
        guest_info:guest,
        pageSize:'5' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  


  

  

  
  

  

  

  


</body>
</html>

